package pattern.structure.bridge;

/**
 * @author : Rookie
 * @description :
 * 将抽象和实现解耦，使它们可以独立地变化。桥接模式使用了“组合优先于继承”的原则，
 * * 通过建立一个桥接接口，将抽象和实现进行解耦*
 * @date : 2023/8/13
 */
public class BridgePattern {

    public static void main(String[] args) {
        OperatingSystem os = new IOS();
        ApplePhone applePhone = new ApplePhone(os);
        applePhone.run();
    }
}

@FunctionalInterface
interface OperatingSystem {
    void run();
}

// 具体的操作系统实现
class IOS implements OperatingSystem {

    @Override
    public void run() {
        System.out.println("运行ios系统");
    }
}

class Android implements OperatingSystem {

    @Override
    public void run() {
        System.out.println("运行android系统");
    }
}

// 手机品牌抽象类
abstract class PhoneBrand {

    protected final OperatingSystem os;

    public PhoneBrand(OperatingSystem os) {
        this.os = os;
    }

    abstract void run();
}

class ApplePhone extends PhoneBrand {

    public ApplePhone(OperatingSystem os) {
        super(os);
    }

    @Override
    void run() {
        System.out.println("运行苹果手机！");
        os.run();
    }
}

class HuaweiPhone extends PhoneBrand {

    public HuaweiPhone(OperatingSystem os) {
        super(os);
    }

    @Override
    void run() {
        System.out.println("运行华为手机！");
        os.run();
    }
}
